#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib || exit 1

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

echo "keyboard-configuration	keyboard-configuration/variant	select	English (US)" | debconf-set-selections

# install packages
install_packages

if [ $PACKAGE_VARIANT = "toolbox" ] || [ $PACKAGE_VARIANT = "ultra" ] || [ $PACKAGE_VARIANT = "puzzle" ]; then
    pkg install xrdp xorgxrdp
    adduser xrdp ssl-cert
    rm /etc/ssl/private/ssl-cert-snakeoil.key /etc/ssl/certs/ssl-cert-snakeoil.pem

    echo "Enable xrdp.service autostart."
    cat <<EOF >/usr/lib/systemd/system/xrdp-configure.service
[Unit]
Description=Configure xrdp service
Before=xrdp.service

[Service]
Type=oneshot
ExecStart=-/bin/sh -c 'if [ ! -f /var/log/xrdp.log ]; then touch /var/log/xrdp.log; chown xrdp:adm /var/log/xrdp.log; chmod 640 /var/log/xrdp.log; fi; if [ ! -f /etc/ssl/private/ssl-cert-snakeoil.key ]; then make-ssl-cert generate-default-snakeoil; fi'
RemainAfterExit=true
StandardOutput=journal

[Install]
WantedBy=multi-user.target
EOF
    systemctl enable xrdp-configure
    systemctl disable xrdp xrdp-sesman
fi

if [ $DESKTOP_ENVIRONMENT = "flux" ]; then
    USER_NAME="root"
fi
cat <<EOF >/usr/lib/systemd/system/xorg.service
[Unit]
Description=X-Window
ConditionKernelCommandLine=!text
After=systemd-user-sessions.service

[Service]
ExecStart=/bin/su --login -c "/usr/bin/startx -- :0 vt7 -ac -nolisten tcp" $USER_NAME

[Install]
WantedBy=graphical.target

EOF

if [ -d /etc/systemd/system ]; then
    mkdir -p /etc/systemd/system
fi
ln -s /usr/lib/systemd/system/xorg.service /etc/systemd/system/display-manager.service
if [ -L /etc/systemd/system/display-manager.service ]; then
    echo "Display manager link exists."
else
    exit 1
fi

# Check if Xorg binary exists before operating on it
if [ -f /usr/lib/xorg/Xorg ]; then
    chmod u+s /usr/lib/xorg/Xorg
else
    error "Xorg binary does not exist."
    exit 1
fi

# Only perform operations if directories exist
if [ -d /usr/share/icons/breeze_cursors ]; then
    rm -Rf /usr/share/icons/breeze_cursors
fi

if [ -d /usr/share/icons/Breeze_Snow ]; then
    mv /usr/share/icons/Breeze_Snow /usr/share/icons/breeze_cursors
fi

cd /tmp

apt-get download x11-xserver-utils

if [ -f x11-xserver-utils*.deb ]; then
    dpkg -x x11-xserver-utils*.deb /tmp/x11utils
    cd /tmp/x11utils
    cp -aR * /
    cd -
    rm -f x11-xserver-utils*.deb
else
    error "x11-xserver-utils deb file does not exist."
    exit 1
fi

if [ -d /usr/share/icons/hicolor/256x256 ]; then
    rm -Rf /usr/share/icons/hicolor/256x256
fi

if [ "$DISTRIBUTION" != "orel" ]; then
    if type update-alternatives >/dev/null 2>&1; then
        update-alternatives --set x-terminal-emulator /usr/bin/xterm
    else
        error "update-alternatives not found."
        exit 1
    fi
fi
